﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaoSH.AppCode;

namespace TaoSH.View
{
    public partial class UserListN : System.Web.UI.Page
    {
        //被选中的人
        public string checkvalue
        {
            get { return Request.QueryString["checkvalue"] ?? ""; }
        }

        //选择范围
        public string strfliterPeoples
        {
            get { return Request.QueryString["fliterpeoples"] ?? ""; }
        }
        public string strAction
        {
            get { return Request.QueryString["Action"] ?? ""; }
        }
        public string strZYCode
        {
            get { return Request.QueryString["ZYCode"] ?? ""; }
        }
        public string strTitle
        {
            get { return Request.QueryString["title"] ?? "选择人员"; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RepBMList.DataSource = GetBMTask();
                RepBMList.DataBind();

                RepBMListCD.DataSource = GetBMTask();
                RepBMListCD.DataBind();
            }
        }

        protected void RepBMList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField hidBM = (HiddenField)e.Item.FindControl("hidBM");
                Repeater RepRYList = (Repeater)e.Item.FindControl("RepRYList");
                DataTable dtUsers = GetBM(hidBM.Value);
                if (dtUsers.Rows.Count > 0 && strAction != "")
                {
                    Fliter(strAction, ref dtUsers);
                }
                RepRYList.DataSource = dtUsers;
                RepRYList.DataBind();
            }
        }
        protected void RepRYList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            //{
            //    HiddenField hidUser = (HiddenField)e.Item.FindControl("hidUserName");
            //    Repeater RepZZJXTasks = (Repeater)e.Item.FindControl("RepZZJXTasks");

            //    RepZZJXTasks.DataSource = new Pro_TaskB().GetDTByCommand(" SELECT *  FROM PRO_TASK WHERE  GETDATE() BETWEEN TASKPLANSTARTDATE AND DateAdd (day,1,TASKPLANENDDATE) AND TASKUSER='" + hidUser.Value + "' ");
            //    RepZZJXTasks.DataBind();
            //}
        }


        //获取部门
        protected DataTable GetBMTask()
        {
            string strSQL = "SELECT DEPTCODE AS   branchcode FROM  JH_Auth_Branch WHERE DEPTCODE NOT IN (0,1)";
            DataTable dtReturn = new JH_Auth_BranchB().GetDTByCommand(strSQL);
            return dtReturn;
        }

        //获取部门人员
        protected DataTable GetBM(string branchcode)
        {
            string strSQL = "SELECT  DISTINCT branchcode, username,zhiwu FROM  JH_Auth_User WHERE branchcode='" + branchcode + "' AND ISUse='Y' ";
            DataTable dtReturn = new JH_Auth_BranchB().GetDTByCommand(strSQL);
            return dtReturn;
        }


        protected DataTable Fliter(string strAction, ref  DataTable dt)
        {
            try
            {
                List<string> ListFliter = new List<string>();
                //指定选择范围
                if (strAction == "ZDRY" && strfliterPeoples!="")
                {
                    ListFliter = strfliterPeoples.SplitTOList(',');
                }

                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    string username = dt.Rows[i]["username"].ToString();
                    if (!ListFliter.Contains(username) && ListFliter.Count>0)
                    {
                        dt.Rows[i].Delete();
                    }
                }
            }
            catch (Exception)
            {
                
            }
           
            return dt;
        }


    }
}